import os
import random
import re
import time
import requests
from lxml import etree
import csv


headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"
}
#定义表头
data = [['房屋面积', '建造时间', "区域", "房价(/㎡)",'总价(万)']]

file_path = "成都各地区房价.csv"
#https://chengdu.anjuke.com/sale/p1/
#https://chengdu.anjuke.com/sale/p2/
pages = int(input('请输入爬取的页数：'))
for i in range(1,pages+1):
    url = f'https://chengdu.anjuke.com/sale/p{i}/'
    # 发送请求
    res = requests.get(url, headers=headers)
    print("正在给" + url + '发送请求')
    # 读取返回的内容
    content = res.text
    # 实例化一个xpath的对象
    tree = etree.HTML(content)
    # 使用XPath提取数据
    list = tree.xpath('//*[@id="esfMain"]/section/section[3]/section[1]/section[2]/div')

    for i in list:
        areas = i.xpath('./a/div[2]/div[1]/section/div[1]/p[2]/text()')[0].replace('\n', '').strip()
        years = i.xpath('./a/div[2]/div[1]/section/div[1]/p[5]/text()')
        location = i.xpath('./a/div[2]/div[1]/section/div[2]/p[2]/span[1]/text()')[0]
        prices = i.xpath('./a/div[2]/div[2]/p[2]/text()')[0].replace('\n', '').strip()
        total = i.xpath('./a/div[2]/div[2]/p[1]/span[1]/text()')[0].replace('\n', '').strip()
        #有的房子没有建造时间，如果没有建造时间就把时间设置为None 方便对数据做处理
        if len(years) > 0:
            years = re.findall('(\d+)', years[0])[0]
        else:
            years = None
        data.append([areas, years, location, prices,total])

    ## 如果文件不存在，创建文件并写入数据
    if not os.path.exists(file_path):
        with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
            csvwriter = csv.writer(csvfile)
            csvwriter.writerows(data)
        print('创建并保存成功')
    else:
        # 如果文件存在，写入新数据
        with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
            csvwriter = csv.writer(csvfile)
            csvwriter.writerows(data)
        print(f"数据已保存到 {file_path} 文件中")

    time.sleep(random.randint(1, 2))






